package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.BuildingSearchBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 楼宇查询
 */
public class BuildingSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.et_BuildinglName)
    EditText et_BuildinglName;
    @BindView(R.id.sp_Structure)
    Spinner sp_Structure;
    @BindView(R.id.et_Location)
    EditText et_Location;
    @BindView(R.id.et_Owner)
    EditText et_Owner;
    @BindView(R.id.et_Person)
    EditText et_Person;
    @BindView(R.id.et_Phone)
    EditText et_Phone;
    //建筑结构
    private List<GlobalBean> eventSourceList;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_building_search);
        setTitleName("楼宇查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        PublicListPest  publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("出租房结构");//建筑结构
    }


    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
        }
    }



    /**
     * 提交
     */
    private void submit() {
        BuildingSearchBean searchBean=new BuildingSearchBean();
        searchBean.setBuildingname(et_BuildinglName.getText().toString().trim());
        searchBean.setBuildingaddress(et_Location.getText().toString().trim());
        searchBean.setOwner(et_Owner.getText().toString().trim());
        searchBean.setResponsibleperson(et_Person.getText().toString().trim());
        searchBean.setPhone(et_Phone.getText().toString().trim());
        GlobalBean bean= (GlobalBean) sp_Structure.getSelectedItem();
        if (bean!=null&&!"-1".equals(bean.getId())){
            searchBean.setBuildingstructuresId(bean.getId());
        }else {
            searchBean.setBuildingstructuresId("");
        }
        MyRxBus.get().post(new MsgEvent<>("BuildingActivity",2,searchBean));
        finish();
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("出租房结构".equals(publicBean.getDomainName())){
            eventSourceList=publicBean.getDicts();
            eventSourceList.add(0,new GlobalBean("未选择","-1"));
            if (eventSourceList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,eventSourceList);
                sp_Structure.setAdapter(adapter);
            }
        }
    }
}
